package com.ericsson.springauth.model;


import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.management.relation.Role;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;


/**
 * Created by IntelliJ IDEA.
 * User: lbonacina
 * Date: 01/02/11
 * Time: 16.07
 */
@Entity
@Table(name = "USERS")
public class User implements UserDetails, Serializable {
// ------------------------------ FIELDS ------------------------------

    @Id
    @GenericGenerator(name = "generator", strategy = "increment")
    @GeneratedValue(generator = "generator")
    private Long id;

    @Version
    @Column(name = "HIBERNATE_OBJECT_VERSION")
    private long hibernateObjectVersion;

    @Column(name = "FIRST_NAME", nullable = false)
    private String firstName;

    @Column(name = "LAST_NAME", nullable = false)
    private String lastName;

    @Column(name = "EMAIL", nullable = true)
    private String email;

    @Column(name = "PHONE", nullable = true)
    private String phone;

    @Column(name = "USERNAME", nullable = true)
    private String username;

    @Column(name = "PASSWORD", nullable = true)
    private String password;

    @Column(name = "ENABLED", nullable = false)
    @Type(type = "true_false")
    private boolean enabled;


    public User() {

    }

    public User(String firstName, String lastName, String email, String phone, String username, String password, boolean enabled) {

        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.phone = phone;
        this.username = username;
        this.password = password;
        this.enabled = enabled;
    }

    public Long getId() {

        return id;
    }

    public void setId(Long id) {

        this.id = id;
    }

    public long getHibernateObjectVersion() {

        return hibernateObjectVersion;
    }

    public void setHibernateObjectVersion(long hibernateObjectVersion) {

        this.hibernateObjectVersion = hibernateObjectVersion;
    }

    public String getFirstName() {

        return firstName;
    }

    public void setFirstName(String firstName) {

        this.firstName = firstName;
    }

    public String getLastName() {

        return lastName;
    }

    public void setLastName(String lastName) {

        this.lastName = lastName;
    }

    public String getEmail() {

        return email;
    }

    public void setEmail(String email) {

        this.email = email;
    }

    public String getPhone() {

        return phone;
    }

    public void setPhone(String phone) {

        this.phone = phone;
    }

    public String getUsername() {

        return username;
    }

    public void setUsername(String username) {

        this.username = username;
    }

    public String getPassword() {

        return password;
    }

    public void setPassword(String password) {

        this.password = password;
    }

    public boolean isEnabled() {

        return enabled;
    }

    public void setEnabled(boolean enabled) {

        this.enabled = enabled;
    }

    @Override
    public String toString() {

        return "User{" +
                "id=" + id +
                ", hibernateObjectVersion=" + hibernateObjectVersion +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", enabled=" + enabled +
                '}';
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
    
        Set<GrantedAuthority> gaSet = new HashSet<GrantedAuthority>();
        gaSet.add(new GrantedAuthorityImpl("ROLE_ADMIN"));        
        return gaSet;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true ;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true ;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true ;
    }
}
